package com.minivision.sdktestdemo.theme.ui;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.theme.utils.CustomThemeInterface;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;
import com.minivision.sdktestdemo.theme.utils.ViewAttributeUtils;

/**
 * Created by Administrator on 2016/6/21.
 */
public class CustomThemeToolbar extends Toolbar implements CustomThemeInterface {
    private int attrBackground = -1;
    private int attrTitleColor = -1;

    public CustomThemeToolbar(Context context) {
        super(context);
    }

    public CustomThemeToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        attrBackground = ViewAttributeUtils.getBackgroundAttribute(attrs);
        getDiyAttrs(context, attrs);
    }

    public CustomThemeToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        attrBackground = ViewAttributeUtils.getBackgroundAttribute(attrs);
        getDiyAttrs(context, attrs);
    }


    @Override
    public void setNavigationIcon(@DrawableRes int resId) {
        super.setNavigationIcon(resId);
        if (getNavigationIcon() != null) {
            getNavigationIcon().setColorFilter(attrTitleColor, PorterDuff.Mode.SRC_ATOP);
        }
    }

    public void getDiyAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(new int[]{R.attr.toolbar_text_color, R.attr.toolbar_nav_color});
        attrTitleColor = typedArray.getColor(0, 0);
        typedArray.recycle();
    }

    public void getDiyAttrs(Resources.Theme theme) {
        TypedArray typedArray = theme.obtainStyledAttributes(new int[]{R.attr.toolbar_text_color, R.attr.toolbar_nav_color});
        attrTitleColor = typedArray.getColor(0, 0);
        typedArray.recycle();
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public void setTheme(Resources.Theme theme) {
        if (attrBackground != -1) {
            if (ViewAttributeUtils.isNeedChangeColor(this)) {
                setBackgroundColor(ThemeUtils.getThemeColor());
            } else {
                ViewAttributeUtils.applyBackgroundDrawable(this, theme, attrBackground);
            }
        }
        getDiyAttrs(theme);
        setTitleTextColor(attrTitleColor);
        if (getNavigationIcon() != null) {
            getNavigationIcon().setColorFilter(attrTitleColor, PorterDuff.Mode.SRC_ATOP);
        }
        if (getOverflowIcon() != null) {
            getOverflowIcon().setColorFilter(attrTitleColor, PorterDuff.Mode.SRC_ATOP);
        }
        if (getMenu() != null) {
//            for (int i = 0; i < getMenu().size(); ++i) {
//                MenuItem item = getMenu().getItem(i);
//                if (item.getTitle().equals(getResources().getString(R.string.search))) {
//                    SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
//                    break;
//                }
//            }
        }
    }
}
